<?php

class PlatformQQSG extends PlatformBase
{
    private $gameId;

    private $updateMoney;

    /**
     * @var PlatformQQ
     */
    private static $instance;

    /**
     * Get the instance of PlatformQQ.
     *
     * @return PlatformQQ
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new PlatformQQSG();
        }
        return self::$instance;
    }

    private function __construct()
    {
        $this->configs = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(self::PLATFORM_QQ), null, true);

        $this->appId = intval($this->configs['appId']['value']);
        $this->requestUrl = $this->configs['requestUrl']['value'];
        $this->appSecret = $this->configs['appSecret']['value'] . '&';//得到密钥的方式：在应用的appkey末尾加上一个字节的“&”，即appkey&
        $this->gameId = intval($this->configs['gameId']['value']);
        $this->updateMoney = [];
    }

    /**
     * 获取平台数据
     * @param $code
     * @return mixed|string
     */
    public static function getCodeMsg($code)
    {
        $config = SzConfig::get()->loadAppConfig(ModuleSns::getSnsConfigFileName(self::PLATFORM_QQ_SG), null, true);
        $params = [
            'appid' => $config['appId']['value'],
            'secret' => $config['v']['value'],
            'js_code' => $code,
            'grant_type' => 'authorization_code'
        ];

        $url = 'https://api.q.qq.com/sns/jscode2session?';
        $try = 5;
        $result = '';
        while (--$try >= 0) {
            try {
                $result = SzUtility::postHttpRequest($url, $params);
                $try = 0;
            } catch (SzException $e) {

            }
        }
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * 验证token
     *
     * @param string $platformId 平台ID
     * @param string $token 登录token
     * @throws SzException
     * @return bool
     */
    public function checkToken($platformId, $token)
    {

    }
}